﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace DependencyAnalysis.DataModel
{
    [Serializable]
    [DataContract(IsReference = true)]
    [KnownType(typeof(ModuleDescriptor))]
    [KnownType(typeof(TypeDescriptor))]
    public class SubmoduleDescriptor : IDescriptor
    {
        private string _identifier;

        [DataMember(Order = 1)]
        public string Identifier
        {
            get { return _identifier; }
            set
            {
                _identifier = value;
                ComparableIdentifier = ("" + _identifier).ToLowerInvariant();
            }
        }

        [DataMember(Order = 2)]
        public string Name { get; set; }
        [DataMember(Order = 3)]
        public string FullName { get; set; }

        public IDescriptor Parent { get { return Module; } }
        [DataMember(Order = 4)]
        public ModuleDescriptor Module { get; set; }

        [DataMember(Order = 5)]
        private readonly IList<TypeDescriptor> _types = new List<TypeDescriptor>();
        public IList<TypeDescriptor> Types
        {
            get { return _types; }
        }

        public override bool Equals(object obj)
        {
            if (!(obj is SubmoduleDescriptor)) return false;

            return string.Equals(ComparableIdentifier, ((SubmoduleDescriptor)obj).ComparableIdentifier);
        }

        internal string ComparableIdentifier { get; set; }

        public override int GetHashCode()
        {
            return ("" + ComparableIdentifier).GetHashCode();
        }

        public override string ToString()
        {
            return GetType().Name + ": " + FullName;
        }
    }
}
